<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Adv;
use App\Models\Banner;
use App\Models\Category;
use App\Models\Config;
use App\Models\Link;
use App\Models\News;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;


class PcController extends BaseController
{


    public function homePage(Request $request) {
        $type = $request->input('type', 1);
        //banner
        $banner_list = Banner::orderBy('sort', 'desc')->where('from', $type)->orderBy("id", "desc")
            ->get();
        //category
        $cat_list = Category::where('parent_id', 0)->withCertain('child', ['id', 'title', 'parent_id'])->orderBy('sort', 'desc')->orderBy("id", "desc")
            ->get();

        //投资金额
        $amount_list = Config::where('key', 'amount')->get();

        //推荐项目前5个
        $project_field = ['id', 'name', 'adv_map', 'amount'];
        $rec_project_list = Project::where('is_rec', 1)
            ->orderBy("sort", "desc")->orderBy("id", "desc")
            ->select($project_field)
            ->limit(5)
            ->get();


        //10个精品专区
        $special_project_list = Project::where('is_special', 1)
            ->orderBy("sort", "desc")->orderBy("id", "desc")
            ->select($project_field)
            ->limit(10)
            ->get();


        //10个新品
        $latest_project_list = Project::orderBy("id", "desc")
            ->select($project_field)
            ->limit(10)
            ->get();


        //加盟排行榜
        $rank_project_list = Project::orderBy('sort', 'desc')
            ->orderBy("pv", "desc")->orderBy("id", "desc")
            ->select($project_field)
            ->limit(10)
            ->get();


        //广告位
        $temp_adv_list = Adv::get()->groupBy('position')->toArray();
        $adv_list = [];
        foreach ($temp_adv_list as $key => $adv) {
            $adv_list[$key] = array_pop($adv);
        }

        //加盟资讯
        $news_type_list = Config::where('key', 'news_type')->limit(4)->get();
        foreach ($news_type_list as $item) {
            $item->news_list = News::where('news_type', $item->id)->select(['id', 'image_url', 'name'])->limit(10)->get();
        }

        //友情连接
        $link_list = Link::get();


        //前9个一级分类
        $nine_cat_list = Category::where('parent_id', 0)->select(['id', 'title'])->orderBy('sort', 'desc')->orderBy("id", "desc")->get();
        $project_field = ['id', 'name', 'adv_map', 'amount'];
        foreach ($nine_cat_list as &$item) {
            $parent_category_id = $item->id;
            $item->rank_project_list = Project::where('parent_category_id', $parent_category_id)->orderBy('sort', 'desc')
                ->orderBy("pv", "desc")->orderBy("id", "desc")
                ->select($project_field)
                ->limit(10)
                ->get();
            break;
        }

        $site = Config::where('key', 'site')->first();
        $site = json_decode($site->value, true);
        return $this->success(compact('banner_list', 'cat_list', 'amount_list', 'rec_project_list', 'special_project_list'
            ,'latest_project_list', 'rank_project_list', 'adv_list', 'link_list', 'site', 'news_type_list', 'nine_cat_list'));
    }

    public function topTenProject(Request $request) {
        $this->validate($request, [
            'parent_category_id' => 'required'
        ]);
        $parent_category_id = $request->input('parent_category_id');
        $project_field = ['id', 'name', 'adv_map', 'amount'];
        $rank_project_list = Project::where('parent_category_id', $parent_category_id)->orderBy('sort', 'desc')
            ->orderBy("pv", "desc")->orderBy("id", "desc")
            ->select($project_field)
            ->limit(10)
            ->get();
        return $this->success($rank_project_list);
    }

    public function projectList(Request $request) {
        //投资金额
        $amount_list = Config::where('key', 'amount')->get();
        //category
        $cat_list = Category::where('parent_id', 0)->withCertain('child', ['id', 'title', 'parent_id'])->orderBy('sort', 'desc')->orderBy("id", "desc")
            ->get();
        //广告位
        $temp_adv_list = Adv::get()->groupBy('position')->toArray();
        $adv_list = [];
        foreach ($temp_adv_list as $key => $adv) {
            $adv_list[$key] = array_pop($adv);
        }

        $input = $request->input();
        $search = ['category_id' => 0, 'parent_category_id' => 0];
        $amount = $request->input('amount');
        $field = ['id', 'name', 'adv_map', 'amount', 'company_title', 'category_id', 'parent_category_id', 'pv'];
        $is_rank = $request->input('is_rank');
        $name = $request->input('name');
        $list = Project::apply($search, $input)
            ->withCertain('category', ['title'])
            ->withCertain('parent_category', ['title'])
            ->when($amount, function ($q) use($amount){
                $q->where('amount', $amount);
            })
            ->when($name, function ($q) use ($name) {
                $q->where('name', 'like', '%' . $name . '%');
            })
            ->orderBy("sort", "desc")->when($is_rank, function ($q) {
                $q->where('is_rank', 1);
            })->orderBy("id", "desc")
            ->select($field)
            ->paginate(per_page());
        foreach ($list as $item) {
            $item->append(['amount_show']);
        }
        $projectList = $this->paginate2($list);


        $project_field = ['id', 'name', 'adv_map', 'amount'];
        //加盟排行榜
        $rank_project_list = Project::orderBy('sort', 'desc')
            ->orderBy("pv", "desc")->orderBy("id", "desc")
            ->select($project_field)
            ->limit(10)
            ->get();



        //加盟优惠
        $field = ['id', 'name'];
        $join_coupon_news_list = News::where('join_coupon', 1)->select($field)->orderBy("sort", "desc")->orderBy("id", "desc")->limit(10)->get();


        //新上线10个项目
        $latest_project_list = Project::orderBy("id", "desc")
            ->withCertain('category', ['title'])
            ->withCertain('parent_category', ['title'])
            ->select(['name', 'company_title', 'id'])
            ->limit(10)
            ->get();

        //热门分类
        $hot_cat_list = Category::where('parent_id', '!=', 0)->orderBy('sort', 'desc')->orderBy("id", "desc")
            ->get();


        //前9个一级分类
        $nine_cat_list = Category::where('parent_id', 0)->select(['id', 'title'])->orderBy('sort', 'desc')->orderBy("id", "desc")->get();
        $project_field = ['id', 'name', 'adv_map', 'amount'];
        foreach ($nine_cat_list as &$item) {
            $parent_category_id = $item->id;
            $item->rank_project_list = Project::where('parent_category_id', $parent_category_id)->orderBy('sort', 'desc')
                ->orderBy("pv", "desc")->orderBy("id", "desc")
                ->select($project_field)
                ->limit(10)
                ->get();
            break;
        }

        return $this->success(compact('amount_list', 'cat_list', 'adv_list', 'projectList', 'rank_project_list'
            , 'join_coupon_news_list', 'latest_project_list', 'hot_cat_list', 'nine_cat_list'));
    }

    //排行榜
    public function rankList() {
        //投资金额
        $amount_list = Config::where('key', 'amount')->get();


        //综合排行榜
        $rank_project_list = Project::orderBy('sort', 'desc')
            ->orderBy("pv", "desc")->orderBy("id", "desc")
            ->select(['id', 'name', 'adv_map', 'amount', 'join_desc'])
            ->limit(10)
            ->get();

        //推荐项目前4个
        $project_field = ['id', 'name', 'adv_map', 'amount'];
        $rec_project_list = Project::where('is_rec', 1)
            ->orderBy("sort", "desc")->orderBy("id", "desc")
            ->select($project_field)
            ->limit(4)
            ->get();

        //热门榜单
        $child_category_list = Category::where('parent_id', '!=', 0)->get();

        //一级分类排行榜
        $parent_category_list = Category::where('parent_id', '=', 0)->get();
        foreach ($parent_category_list as &$item) {
            $item->project_list = Project::orderBy('sort', 'desc')
                ->orderBy("pv", "desc")->orderBy("id", "desc")
                ->select(array_merge($project_field, ['join_desc']))
                ->limit(10)
                ->get();
        }

        //资讯前10
        $news_type_list = Config::where('key', 'news_type')->limit(4)->get();
        $field = ['id', 'name', 'created_at', 'image_url', 'short_content'];
        foreach ($news_type_list as &$item) {
            $item->news_list = News::where('news_type', $item->id)->select($field)->orderBy("sort", "desc")->orderBy("id", "desc")->limit(10)->get();
        }

        //前9个一级分类
        $nine_cat_list = Category::where('parent_id', 0)->select(['id', 'title'])->orderBy('sort', 'desc')->orderBy("id", "desc")->get();
        $project_field = ['id', 'name', 'adv_map', 'amount'];
        foreach ($nine_cat_list as &$item) {
            $parent_category_id = $item->id;
            $item->rank_project_list = Project::where('parent_category_id', $parent_category_id)->orderBy('sort', 'desc')
                ->orderBy("pv", "desc")->orderBy("id", "desc")
                ->select($project_field)
                ->limit(10)
                ->get();
            break;
        }

        return $this->success(compact('amount_list', 'rank_project_list', 'rec_project_list', 'child_category_list'
            ,'parent_category_list', 'news_type_list', 'nine_cat_list'));
    }


    public function projectInfo(Request $request)
    {
        $model = new Project();
        $table = $model->getTable();
        $this->validate($request, [
            'id' => 'required|exists:' . $table . ",id"
        ]);
        $id = \request("id");
        $info = $model->withCertain('category', ['title'])
            ->withCertain('parent_category', ['title'])->find($id);
        if (!$info)
            return $this->failed('项目不存在');
        $info->append(['amount_show', 'tag_ids_show', 'target_user_show', 'manage_mode_show', 'company_type_show']);
        $cache_key = 'project_' . $id;
        $pv = Redis::incr($cache_key, 1);
        if ($pv % 10 == 0) {
            $info->pv = $pv;
            $info->save();
        }

        //xx加盟项目
        $project_field = ['id', 'name', 'adv_map', 'amount', 'logo'];
        $relate_project_list = Project::where('parent_category_id', $info->parent_category_id)
            ->where('id', "!=", $id)
            ->where('is_rec', 1)
            ->orderBy("sort", "desc")->orderBy("id", "desc")
            ->select($project_field)
            ->limit(4)
            ->get();


        //资讯-6
        $field = ['id', 'name', 'created_at', 'image_url'];
        $relate_news_list = News::where('parent_category_id', $info->parent_category_id)->select($field)
            ->orderBy("sort", "desc")->orderBy("id", "desc")->limit(6)->get();

        //餐饮排行榜
        $rank_project_list = Project::where('parent_category_id', $info->parent_category_id)
            ->orderBy("sort", "desc")->orderBy('pv', 'desc')->orderBy("id", "desc")
            ->select($project_field)
            ->limit(4)->get();

        //热门资讯
        $hotest_news_list = News::select($field)->orderBy("pv", "desc")->orderBy("id", "desc")->limit(6)->get();

        //xx品牌咨询
        $project_news_list = News::where('project_id', $info->id)->select($field)->orderBy("sort", "desc")->orderBy("id", "desc")->limit(6)->get();

        //最新资讯
        $latest_news_list = News::select($field)->orderBy("id", "desc")->limit(6)->get();

        //category
        $cat_list = Category::where('parent_id', 0)->orderBy('sort', 'desc')->orderBy("id", "desc")
            ->get();

        //前9个一级分类
        $nine_cat_list = Category::where('parent_id', 0)->select(['id', 'title'])->orderBy('sort', 'desc')->orderBy("id", "desc")->get();
        $project_field = ['id', 'name', 'adv_map', 'amount'];
        foreach ($nine_cat_list as &$item) {
            $parent_category_id = $item->id;
            $item->rank_project_list = Project::where('parent_category_id', $parent_category_id)->orderBy('sort', 'desc')
                ->orderBy("pv", "desc")->orderBy("id", "desc")
                ->select($project_field)
                ->limit(10)
                ->get();
            break;
        }

        //投资金额
        $amount_list = Config::where('key', 'amount')->get();

        return $this->success(compact('info', 'relate_news_list', 'relate_project_list', 'rank_project_list', 'hotest_news_list',
            'project_news_list', 'latest_news_list', 'cat_list', 'nine_cat_list', 'amount_list'));
    }


    public function newsList() {
        //category
        $cat_list = Category::where('parent_id', 0)->orderBy('sort', 'desc')->orderBy("id", "desc")
            ->get();
        //投资金额
        $amount_list = Config::where('key', 'amount')->get();
        //分类
        $news_type_cat_list = Config::where('key', 'news_type')->get();
        //行业热点聚焦
        $focus_news_list = News::where('is_focus', 1)->select(['id', 'name', 'image_url'])->orderBy('sort', 'desc')->orderBy("id", "desc")->limit(5)->get();
        //资讯首页1
        $homepage_1_news_list = News::where('homepage_1', 1)->select(['id', 'name'])->orderBy('sort', 'desc')->orderBy("id", "desc")->limit(5)->get();
        //资讯首页2
        $homepage_2_news_list = News::where('homepage_2', 1)->select(['id', 'name'])->orderBy('sort', 'desc')->orderBy("id", "desc")->limit(5)->get();
        //资讯首页3
        $homepage_3_news_list = News::where('homepage_3', 1)->select(['id', 'name'])->orderBy('sort', 'desc')->orderBy("id", "desc")->limit(10)->get();
        $field = ['id', 'name', 'adv_map', 'amount'];
        //品牌资讯
        $brand_project_list = Project::where('is_brand_news', 1)
            ->orderBy("sort", "desc")->orderBy("id", "desc")
            ->select($field)
            ->limit(4)->get();
        //加盟优惠
        $join_coupon_news_list = News::where('join_coupon', 1)->select(['id', 'name'])->orderBy('sort', 'desc')->orderBy("id", "desc")->limit(5)->get();

        //前面四个资讯
        $news_type_list = Config::where('key', 'news_type')->limit(4)->get();
        foreach ($news_type_list as $item) {
            $item->news_list = News::where('news_type', $item->id)->select(['id', 'image_url', 'name'])->limit(10)->get();
        }

        //行业资讯
        $parent_category_list = Category::where('parent_id', '=', 0)->get();
        foreach ($parent_category_list as &$item) {
            $item->news_list = News::orderBy("id", "desc")
                ->select(['id', 'name'])
                ->limit(10)
                ->get();

        }

        //前9个一级分类
        $nine_cat_list = Category::where('parent_id', 0)->select(['id', 'title'])->orderBy('sort', 'desc')->orderBy("id", "desc")->get();
        $project_field = ['id', 'name', 'adv_map', 'amount'];
        foreach ($nine_cat_list as &$item) {
            $parent_category_id = $item->id;
            $item->rank_project_list = Project::where('parent_category_id', $parent_category_id)->orderBy('sort', 'desc')
                ->orderBy("pv", "desc")->orderBy("id", "desc")
                ->select($project_field)
                ->limit(10)
                ->get();
            break;
        }

        return $this->success(compact('cat_list', 'amount_list', 'news_type_cat_list', 'focus_news_list', 'homepage_1_news_list',
            'homepage_2_news_list', 'homepage_3_news_list', 'brand_project_list', 'join_coupon_news_list', 'news_type_list', 'parent_category_list', 'nine_cat_list'));
    }


    public function news_child(Request $request) {
        $this->validate($request, [
           'news_type' => 'required'
        ]);
        $news_type = $request->input('news_type');
        $info = Config::find($news_type);
        if (!$info || $info->key != 'news_type')
            return $this->failed('不存在');

        $value = $info->value;
        //投资金额
        $amount_list = Config::where('key', 'amount')->get();
        //分类
        $news_type_cat_list = Config::where('key', 'news_type')->get();

        $news_list = News::where('news_type', $news_type)->select(['id', 'name','short_content', 'created_at'])->orderBy('sort', 'desc')
            ->orderBy("id", "desc")->paginate(per_page());
        $news_list = $this->paginate2($news_list);

        //推荐阅读
        $rec_list = News::where('news_type', $news_type)->where('is_rec_read', 1)->select(['id', 'name'])->orderBy('sort', 'desc')
            ->orderBy("id", "desc")->limit(10)->get();

        //热门资讯
        $hotest_list = News::select(['id', 'name'])->orderBy('pv', 'desc')
            ->orderBy("id", "desc")->limit(10)->get();

        //最新资讯
        $latest_list = News::where('is_rec_read', 1)->select(['id', 'name'])->orderBy("id", "desc")->limit(10)->get();

        return $this->success(compact('value','amount_list', 'news_type_cat_list', 'news_list', 'rec_list', 'hotest_list', 'latest_list'));

    }

    public function newsInfo(Request $request) {
        $id = \request("id");
        $info =  News::with(['project'=>function($q) {
            $q->select(['id', 'name', 'category_id', 'parent_category_id', 'company_title', 'amount']);
        }, 'project.category' => function($q) {
            $q->select(['id', 'title']);
        }, 'project.parent_category' => function($q) {
            $q->select(['id', 'title']);
        }])->find($id);

        if (!$info)
            return $this->failed('不存在');

        $info->append(['news_type_show', 'news_tag_show']);

        $cache_key = 'news_' . $id;
        $pv = Redis::incr($cache_key, 1);
        if ($pv % 10 == 0) {
            $info->pv = $pv;
            $info->save();
        }


        $category_id = $info->project->parent_category_id;
        //xx加盟项目
        $field = ['id', 'name', 'adv_map', 'amount', 'logo'];
        $relate_project_list = Project::where('parent_category_id', $category_id)
            ->where('id', "!=", $id)
            ->where('is_rec', 1)
            ->orderBy("sort", "desc")->orderBy("id", "desc")
            ->select($field)
            ->limit(4)
            ->get();

        //xx资讯
        $field = ['id', 'name'];
        $relate_news_list = News::where('parent_category_id', $category_id)->select($field)
            ->orderBy("sort", "desc")->where('id', '!=', $id)->orderBy("id", "desc")->paginate(per_page(10));
        $relate_news_list =  $this->paginate2($relate_news_list);

        //新入驻品牌
        $project_field = ['id', 'name', 'adv_map', 'amount', 'logo'];
        $latest_project_list = Project::where('parent_category_id', $category_id)
            ->orderBy("id", "desc")
            ->select($project_field)
            ->limit(10)
            ->get();

        //投资金额
        $amount_list = Config::where('key', 'amount')->get();

        return $this->success(compact('info', 'relate_project_list', 'relate_news_list', 'latest_project_list', 'amount_list'));
    }

}

